package com.example.gc.oams.zpx.voucher.util;

import com.example.gc.oams.zpx.voucher.model.AccountingFlowDetail;
import com.thames.util.BeanUtil;
import com.thames.util.StringUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public final class AccountingFlowDetailUtil {

    /**
     * 数据库字段
     */
    public static final List<String> FIELDS_SORT = new ArrayList<>();

    /**
     * 数据库字段映射对象属性
     */
    public static final Map<String, String> FIELD_TO_ATTRIBUTES = new ConcurrentHashMap<>();

    static {
        FIELDS_SORT.add("line_num");
        FIELDS_SORT.add("account_voucher_no");
        FIELDS_SORT.add("amount");
        FIELDS_SORT.add("currency");
        FIELDS_SORT.add("exchange_rate");
        FIELDS_SORT.add("ledger_account");
        FIELDS_SORT.add("sub_type");
        FIELDS_SORT.add("address_number");
        FIELDS_SORT.add("accounting_date");
        FIELDS_SORT.add("sub_dis");
        FIELDS_SORT.add("remark");
        FIELDS_SORT.add("jde_remark");
        FIELDS_SORT.add("fail_remark");
        FIELDS_SORT.add("gen_status");
        FIELDS_SORT.add("create_time");
        FIELDS_SORT.add("create_user");
        FIELDS_SORT.add("modify_time");
        FIELDS_SORT.add("modify_user");
        FIELDS_SORT.add("company_no");
        FIELDS_SORT.add("jde_account");

        FIELD_TO_ATTRIBUTES.put("line_num", "lineNum");
        FIELD_TO_ATTRIBUTES.put("account_voucher_no", "accountVoucherNo");
        FIELD_TO_ATTRIBUTES.put("amount", "amount");
        FIELD_TO_ATTRIBUTES.put("currency", "currency");
        FIELD_TO_ATTRIBUTES.put("exchange_rate", "exchangeRate");
        FIELD_TO_ATTRIBUTES.put("ledger_account", "ledgerAccount");
        FIELD_TO_ATTRIBUTES.put("sub_type", "subType");
        FIELD_TO_ATTRIBUTES.put("address_number", "addressNumber");
        FIELD_TO_ATTRIBUTES.put("accounting_date", "accountingDate");
        FIELD_TO_ATTRIBUTES.put("sub_dis", "subDis");
        FIELD_TO_ATTRIBUTES.put("remark", "remark");
        FIELD_TO_ATTRIBUTES.put("jde_remark", "jdeRemark");
        FIELD_TO_ATTRIBUTES.put("fail_remark", "failRemark");
        FIELD_TO_ATTRIBUTES.put("gen_status", "genStatus");
        FIELD_TO_ATTRIBUTES.put("create_time", "createTime");
        FIELD_TO_ATTRIBUTES.put("create_user", "createUser");
        FIELD_TO_ATTRIBUTES.put("modify_user", "modifyUser");
        FIELD_TO_ATTRIBUTES.put("modify_time", "modifyTime");
        FIELD_TO_ATTRIBUTES.put("company_no", "companyNo");
        FIELD_TO_ATTRIBUTES.put("jde_account", "jdeAccount");
    }

    /**
     * 生成SQL语句
     *
     * @param accountingFlowDetail
     * @return
     */
    public static String genAccountingFlowDetailSqlScript(AccountingFlowDetail accountingFlowDetail) {
        StringBuffer sb = new StringBuffer();
        sb.append("insert into capital_qa.accounting_flow_detail(");
        sb.append(StringUtil.join(FIELDS_SORT)).append(") values(");

        Map<String, Object> flowMap = BeanUtil.asMap(accountingFlowDetail);
        for (int i = 0; i < FIELDS_SORT.size(); i++) {
            if (i != 0) {
                sb.append(",");
            }

            Object value = flowMap.get(FIELD_TO_ATTRIBUTES.get(FIELDS_SORT.get(i)));
            if (value == null) {
                sb.append("null");
            } else if (value instanceof String) {
                sb.append("'").append(value.toString()).append("'");
            } else if (value instanceof Date) {
                sb.append("'").append(StringUtil.format(VoucherUtils.YYYY_MM_DD_HH_MM_SS_2, (Date) value)).append("'");
            } else {
                sb.append(value.toString());
            }
        }

        sb.append(");\n");

        return sb.toString();
    }


    private AccountingFlowDetailUtil() {
    }

}
